package com.base.cn.platform.os.service.manage.login;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.web.UserAgentUtil;
import com.base.cn.platform.os.dao.manage.login.SysLoginLogDao;
import com.base.cn.platform.os.entity.manage.login.SysLoginLog;
import com.base.cn.platform.os.entity.manage.user.SysUser;
import com.base.cn.platform.os.service.manage.user.SysUserBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * 用户登录日志
 *
 * @author s.li
 * @create 2018-02-05 16:31
 */
@Service
public class SysLoginLogBiz extends BaseBiz<SysLoginLog,SysLoginLogDao> {
    @Autowired
    private SysUserBiz sysUserBiz;

    /**
     * 保存登录日志
     * @param request
     * @param sysUser
     */
    public void saveSysLoginLog(HttpServletRequest request, SysUser sysUser) {
        UserAgentUtil util = new UserAgentUtil(request);
        SysLoginLog sysLoginLog = new SysLoginLog();
        sysLoginLog.setUserId(sysUser.getId());
        sysLoginLog.setUserAgent(util.getBrowserName());
        sysLoginLog.setOsName(util.getOSName());
        sysLoginLog.setLoginIp(util.getUserIp());
        sysLoginLog.setAddress(util.getUserAddress());
        this.save(sysLoginLog);
    }

    /**
     * 查询登录日志列表
     * @param sysLoginLog 查询条件
     * @param page 分页条件
     * @param sysUserData 是否是设置用户数据
     * @return PageInfo<SysLoginLog>
     */
    public PageInfo<SysLoginLog> findSysLoginListPage(SysLoginLog sysLoginLog, Pagination page,
                                                      boolean sysUserData){
        String whereSql = getWhereSql(sysLoginLog);
        PageInfo<SysLoginLog> sysLoginLogList = this.findPage(whereSql,page,null);
        if(ObjectUtils.isNotEmpty(sysLoginLogList.getList())){
            sysLoginLogList.setList(this.setSysUserData(sysLoginLogList.getList(),sysUserData));
        }
        return sysLoginLogList;
    }

    //拼接sql语句
    private String getWhereSql(SysLoginLog sysLoginLog){
        String whereSql = "1=1";
        if(ObjectUtils.isNotEmpty(sysLoginLog)){
            if(ObjectUtils.isNotEmpty(sysLoginLog.getUserId())){
                whereSql += " and userId="+ sysLoginLog.getUserId();
            }
            if(StringUtils.isNotEmpty(sysLoginLog.getStartTime())){
                whereSql += " and createTime >='" + sysLoginLog.getStartTime() + "'";
            }
            if(StringUtils.isNotEmpty(sysLoginLog.getEndTime())){
                whereSql += " and createTime <='" + sysLoginLog.getEndTime() + "'";
            }
        }
        whereSql += " order by id desc";
        return whereSql;
    }

    /**
     * 设置用户数据
     * @param sysLoginLogList
     * @param sysUserData
     * @return
     */
    private List<SysLoginLog> setSysUserData(List<SysLoginLog> sysLoginLogList,boolean sysUserData){
        if(sysUserData && ObjectUtils.isNotEmpty(sysLoginLogList)){
            String userIds = sysLoginLogList.stream().map(e->e.getUserId().toString()).collect(Collectors.joining(","));
            Map<BigDecimal,SysUser> sysUserMap = sysUserBiz.findSysUserByIds(userIds);
            sysLoginLogList.forEach(e->{
                SysUser sysUser = sysUserMap.get(e.getUserId());
                e.setSysUser(sysUser);
            });
        }
        return sysLoginLogList;
    }
}
